DEBUG_COLLISIONS = true;
var BASE_URL = '';
var LIBS = ['js/jquery-1.6.1.min.js', 'js/jquery-css-transform.js', 'js/utils.js', 'js/sprite.js', 'js/world.js', 'js/droid.js', 'js/terrain.js', 'js/map2d.js',
		'js/terrain_manager.js', 'js/ui.js', 'js/droid_action.js', 'js/entrance_exit.js', 'js/overlay_utils.js', 'js/droid_selection.js', 'js/derived_droids.js', 'js/button.js'];

function blockEvent(event) {
	if (event && event.preventDefault) {
		event.preventDefault();
	} else if (window.event && window.event.returnValue) {
		window.eventReturnValue = false;
	}
	event.returnValue = false;
	return false;
}

function DroidCrawl() {
	this.world;
	this.ui;

	this.frameRateTime;
	this.frames;
	this.lastFrame;
	this.thisFrame;
	this.timeDelta;
	this.accumulator;
	this.frameTime;


	this.init = function() {
		this.world = new World();
		this.ui = new UI(this.world);
		window.scrollTo(0, 0);

		$(document).mousemove(function(e) {
			window.droidcrawl.ui.mouseMove(e.pageX, e.pageY);
		});

		$(document).mouseup(function(e) {
			window.droidcrawl.ui.mouseUp(e.pageX, e.pageY);
		});
		
		$(document).mousedown(function(e) {
      window.droidcrawl.ui.mouseDown(e.pageX, e.pageY);
    });

		// sets the player to the first droid
		var player = window.droidcrawl.world.droids[0];
		document.onkeydown = function(event) {
			switch (event.keyCode) {
			case 65:
				player.goLeft();
				break;
			case 68:
				player.goRight();
				break;
			case 83:
				player.drill();
				break;
			case 87:
				player.block();
				break;
			case 69:
				player.jump();
				break;
			case 81:
				window.droidcrawl.world.click();
				break;
			}
		}
		document.onkeyup = function(event) {
			switch (event.keyCode) {
			case 65:
				player.stop();
				break;
			case 68:
				player.stop();
				break;
			case 83:
				player.stop();
				break;
			}
		}

		// this.convertDocument(document.body);

		this.frameRateTime = new Date().getTime();
		this.frames = 0;
		this.lastFrame = new Date().getTime();
		this.thisFrame;
		this.timeDelta = 45;
		this.accumulator = 0;
		this.frameTime;
		this.step();
		// simulationInterval = setInterval("window.droidcrawl.step()", 40);
		// simulationInterval = setInterval(function() {
		// window.droidcrawl.updatePhysics();
		// window.droidcrawl.redraw();
		// }, 40);
	}

	this.step = function() {
		this.frames++;
		this.thisFrame = new Date().getTime();
		this.frameTime = this.thisFrame - this.lastFrame;
		// Log.i("delta", "delta: " + (thisFrame - lastFrame));
		this.lastFrame = this.thisFrame;
		this.accumulator += this.frameTime;

		// while (this.accumulator >= this.timeDelta) {
		
		this.world.clear();
		this.world.step();
		this.world.draw();
		this.ui.draw();
		
		this.accumulator -= this.timeDelta;
		// }

		var elapsed = this.thisFrame - this.frameRateTime;
		if (elapsed > 5000) {
			var framerate = this.frames / (elapsed / 1000);
			console.log("Framerate: " + framerate + " fps");
			this.frames = 0;
			this.frameRateTime = new Date().getTime();
		}

		var frameEnd = new Date().getTime();
		if (!this.world.statistics.hasFinished()) {
			setTimeout("window.droidcrawl.step()", Math.max(40 - (frameEnd - this.thisFrame), 0));
		}
	}
}

function loadLibs() {
	if (LIBS.length > 0) {
		// we still have unloaded libs
		var lib = BASE_URL + LIBS.shift()
		console.log("Main, loading lib " + lib)

		var s = document.createElement('script');
		s.src = lib;
		document.body.appendChild(s);
		setTimeout(loadLibs, 50);
	} else {
		// forward into our main function
		setTimeout(main, 1000);

	}
}

function main(foo, bar) {
	console.log("in main!" + window.droidcrawl)
	if (!window.droidcrawl) {
		window.droidcrawl = new DroidCrawl();
	}

	// TODO(caskey) somehow delay until all the other lib scripts
	window.droidcrawl.init()
}

loadLibs();
